/*
 * 文件名： DriveMotorInfo.java
 * 
 * 创建日期： 2016年12月1日
 *
 * Copyright(C) 2016, by <a href="mailto:liws@xingyuanauto.com">liws</a>.
 *
 * 原始作者: liws
 *
 */
package com.moli.iov.protocol.model.realtime;

import java.io.Serializable;

import com.moli.iov.protocol.codec.DataDecoder;
import com.moli.iov.protocol.codec.DataEncoder;
import com.moli.iov.protocol.codec.Serialize;
import com.moli.iov.protocol.enums.realtime.DriveMotorStateTag;
import com.moli.iov.protocol.model.EntityBytesLen;
import com.moli.iov.util.ByteArrayUtil;


/**
 * 驱动电机 信息实体 与DriveMotorData为 多对一 关系
 * 
 *
 * @author <a href="mailto:liws@xingyuanauto.com">liws</a>
 *
 * @version $Revision$
 *
 * @since 2016年12月1日
 */
@Serialize(unit="byte",isBigEndian=true)
public class DriveMotorInfo implements Serializable,EntityBytesLen{

	 /** 
	 */ 
	public static final long serialVersionUID = 1L;
	

	 /**
	 *
	 */
	@Override
	public int getBytesLen() {
		return 12;
	}
	/**
	 * 电机序号
	 */
	@Serialize(unit="byte",offset=0,size=1,min=1,max=253)
	public short motorSerialNum;
	
	/**
	 * 驱动电机状态
	 */
	@Serialize(unit="byte",offset=1,size=1)
	public DriveMotorStateTag motorState;
	
	/**
	 * 驱动电机控制器温度
	 */
	@Serialize(unit="byte",offset=2,size=1,valOffset=40,min=1,max=250)
	public short motorControlTemperature;
	
	/**
	 * 驱动电机转速
	 */
	@Serialize(unit="byte",offset=3,size=2,valOffset=20000,min=0,max=65531)
	public int motorRotateSpeed;
	
	/**
	 * 驱动电机转矩
	 */
	@Serialize(unit="byte",offset=5,size=2,valOffset=2000,min=0,max=65531,coefficient=0.1)
	public double motorTorque;
	
	/**
	 * 驱动电机温度
	 */
	@Serialize(unit="byte",offset=7,size=1,valOffset=40,min=0,max=250)
	public short motorTemperature;
	
	/**
	 * 驱动电机控制器输入电压
	 */
	@Serialize(unit="byte",offset=8,size=2,min=0,max=60000,coefficient=0.1)
	public double motorControlVoltage;
	
	/**
	 * 驱动电机控制器直流母线电流
	 */
	@Serialize(unit="byte",offset=10,size=2,valOffset=1000,min=0,max=20000,coefficient=0.1)
	public double motorControlCurrent;

	/**
	*
	* @return the motorSerialNum
	*/
	
	public short getMotorSerialNum() {
		return motorSerialNum;
	}

	/**
	*
	* @param motorSerialNum the motorSerialNum to set
	*/
	public void setMotorSerialNum(short motorSerialNum) {
		this.motorSerialNum = motorSerialNum;
	}

	public DriveMotorStateTag getMotorState() {
		return motorState;
	}

	public void setMotorState(DriveMotorStateTag motorState) {
		this.motorState = motorState;
	}

	public short getMotorControlTemperature() {
		return motorControlTemperature;
	}

	public void setMotorControlTemperature(short motorControlTemperature) {
		this.motorControlTemperature = motorControlTemperature;
	}

	public int getMotorRotateSpeed() {
		return motorRotateSpeed;
	}

	public void setMotorRotateSpeed(int motorRotateSpeed) {
		this.motorRotateSpeed = motorRotateSpeed;
	}

	public double getMotorTorque() {
		return motorTorque;
	}

	public void setMotorTorque(double motorTorque) {
		this.motorTorque = motorTorque;
	}

	public short getMotorTemperature() {
		return motorTemperature;
	}

	public void setMotorTemperature(short motorTemperature) {
		this.motorTemperature = motorTemperature;
	}

	public double getMotorControlVoltage() {
		return motorControlVoltage;
	}

	public void setMotorControlVoltage(double motorControlVoltage) {
		this.motorControlVoltage = motorControlVoltage;
	}

	public double getMotorControlCurrent() {
		return motorControlCurrent;
	}

	public void setMotorControlCurrent(double motorControlCurrent) {
		this.motorControlCurrent = motorControlCurrent;
	}
	
	 /**
	 *
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("DriveMotorInfo [motorSerialNum=");
		builder.append(motorSerialNum);
		builder.append(", motorState=");
		builder.append(motorState);
		builder.append(", motorControlTemperature=");
		builder.append(motorControlTemperature);
		builder.append(", motorRotateSpeed=");
		builder.append(motorRotateSpeed);
		builder.append(", motorTorque=");
		builder.append(motorTorque);
		builder.append(", motorTemperature=");
		builder.append(motorTemperature);
		builder.append(", motorControlVoltage=");
		builder.append(motorControlVoltage);
		builder.append(", motorControlCurrent=");
		builder.append(motorControlCurrent);
		builder.append("]");
		return builder.toString();
	}

	public static void main(String[] args) {
		DriveMotorInfo dmi = new DriveMotorInfo();
		dmi.setMotorSerialNum((short)254);
		dmi.setMotorState(DriveMotorStateTag.POWER_GENERATING);
		dmi.setMotorControlTemperature((byte)233);
		dmi.setMotorRotateSpeed((short)60000);
		dmi.setMotorTorque((short)60000);
		dmi.setMotorTemperature((byte)249);
		//dmi.setMotorControlVoltage((short)60000);
		dmi.setMotorControlVoltage(77.87);
		dmi.setMotorControlCurrent((short)20000);
		
		DataEncoder<DriveMotorInfo> encoder = new DataEncoder<DriveMotorInfo>();
		byte[] bytes = null;
		try {
			bytes = encoder.encode1(dmi);
			System.out.println(ByteArrayUtil.toHexString(bytes));
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		DataDecoder<DriveMotorInfo> decoder = new DataDecoder<>();
		try {
			decoder.decode(dmi, bytes);
			System.out.println(dmi.getMotorControlVoltage());
			
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
